/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.utility;

import com.vng.zing.survey.thrift.model.AnswerItem;
import com.vng.zing.survey.thrift.model.QuesType;
import com.vng.zing.survey.thrift.model.Question;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author kienpd
 */
public class Utility {

    public static String renderPagination(int postion, int numberpage) {

        return "abc";
    }

    public static AnswerItem getAnswerItemById(short id, List<AnswerItem> list) {
        AnswerItem answerItem = null;
        for (AnswerItem ai : list) {
            if (ai.getIdQues() == id) {
                answerItem = new AnswerItem(ai);
                return answerItem;
            }
        }

        return answerItem;
    }

    public static boolean validResponse(List<AnswerItem> answers, List<Question> questions) {
        AnswerItem answerItem;
        for (Question question : questions) {
            answerItem = getAnswerItemById(question.getIdQues(), answers);
            if (question.require) {
                if (answerItem == null) {
                    return false;
                }
                if (question.getType() != answerItem.getType()) {
                    return false;
                }
                if ("".equals(answerItem.getAnswer()) || answerItem.getAnswer() == null) {
                    return false;
                }

                if (question.getType() == QuesType.SINGLE_SELECTION || question.getType() == QuesType.MULTI_CHOICE) {
                    String[] ans = answerItem.getAnswer().split(",");
                    if (question.getChoices().size() != ans.length) {
                        return false;
                    }
                    if (!answerItem.getAnswer().contains("1")) {
                        return false;
                    }
                    int i = 0;
                    for (String an : ans) {
                        if (!an.matches("[0-1]")) {
                            return false;
                        }
                        if (an.equals("1") && question.getType() == QuesType.SINGLE_SELECTION) {
                            i++;

                        }
                    }
                    if (question.getType() == QuesType.SINGLE_SELECTION && i > 1) {
                        return false;
                    }

                }

            } else {
                if (answerItem != null) {
                    if (question.getType() != answerItem.getType()) {
                        return false;
                    }

                    if (question.getType() == QuesType.SINGLE_SELECTION || question.getType() == QuesType.MULTI_CHOICE) {
                        String[] ans = answerItem.getAnswer().split(",");
                        if (question.getChoices().size() != ans.length) {
                            return false;
                        }
                        int i = 0;
                        for (String an : ans) {
                            if (!an.matches("[0-1]")) {
                                return false;
                            }
                            if (an.equals("1") && question.getType() == QuesType.SINGLE_SELECTION) {
                                i++;

                            }
                        }
                        if (question.getType() == QuesType.SINGLE_SELECTION && i > 1) {
                            return false;
                        }

                    }

                }
            }

        }

        return true;
    }
    public static List<String> makeColor(int length){
        List<String> list = new ArrayList<String>();
        return list;
    }

}
